package com.mspbots.core.wise.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mspbots.core.AbstractBaseController;
import com.mspbots.core.wise.model.Tenant;
import com.mspbots.core.wise.service.TenantService;
import com.mspbots.cw.request.TenantDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

@RestController
@RequestMapping("tenant")
public class TenantController extends AbstractBaseController<Tenant, TenantService> {

    @GetMapping("microsoft-id")
    public Callable microsoft(Tenant entity) {
        Assert.notNull(entity.getMicrosoftId(), "microsoft_id can't be null");
        return () -> service.getOne(Wrappers.<Tenant>lambdaQuery().eq(Tenant::getMicrosoftId, entity.getMicrosoftId()));
    }

    @Override
    @GetMapping
    public Callable all() {
        return () -> service.findByAll();
    }

    @Override
    @PostMapping
    public Callable insert(@RequestBody Tenant entity) {
        return () -> {
            service.saveFirst(entity);
            return entity;
        };
    }

    @GetMapping("active")
    public Callable allActive() {
        return () -> service.findByActive();
    }


    @GetMapping("by-status")
    public Callable byStatus() {
        return () -> this.service.findByStatus();
    }

    @GetMapping("name/{short_name}")
    public Callable addAll(@PathVariable(name = "short_name") String name) {
        QueryWrapper<Tenant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("short_name", name);
        return () -> this.service.getOne(queryWrapper);
    }

    @PostMapping("add-batch")
    public Callable addAll(@RequestBody List<TenantDTO> dtos) {
        return () -> {
            List<Tenant> tenants = dtos.stream().map(e -> {
                Tenant tenant = new Tenant();
                BeanUtils.copyProperties(e, tenant);
                return tenant;
            }).collect(Collectors.toList());

            this.service.saveBatch(tenants);
            return dtos;
        };
    }

    @GetMapping("delete-tenant/{tenantId}")
    public Callable<Boolean> deleteTenant(@PathVariable long tenantId) {
        return () -> this.service.deleteTenant(tenantId);
    }


    @GetMapping("exist-tenant/{microsoftId}")
    public Callable existTenant(@PathVariable String microsoftId) {
        return () -> this.service.existTenant(microsoftId);
    }


    @PostMapping("ids-active")
    public Callable findByIdsAndActive(@RequestBody List<Long> ids) {
        if (!Objects.isNull(ids) && ids.size() > 0) {
            return () -> this.service.findByIdsAndActive(ids);
        } else {
            return () -> this.service.findByActive();
        }
    }

    @PutMapping(path = "teams-authority/{id}")
    public Callable teamsAuthority(@PathVariable Long id, @RequestBody Tenant entity) {
        if (entity.getTeamsAuthority() == null) {
            return () -> false;
        }
        return () -> this.service.teamsAuthority(id, entity.getTeamsAuthority());
    }
}
